package com.itita.weibo.model;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.httpclient.util.DateUtil;

import weibo4andriod.Status;
import weibo4andriod.User;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.itita.weibo.manager.MainService;
import com.itita.weibo.ui.R;
import com.itita.weibo.util.WeiboUtil;

/**
 * 微博信息适配器
 * 
 * @author 焦明 2010-5-5 00:10
 *
 */
public final class RecommendStatusAdapter extends BaseAdapter {
	
	public List<Status> allstatus;
	
	private Context context;

	public RecommendStatusAdapter(Context con, List<Status> list) {
		this.allstatus = list;
		this.context = con;
	}

	public int getCount() {
		return allstatus.size() + 2;
	}

	public Object getItem(int position) {
		if (position == 0){
			return null;
		}else if (position == this.getCount() - 1){
			return null;
		}
		// 返回这条微薄对象
		else{
			return allstatus.get(position - 1);
		}
	}

	public long getItemId(int position) {
		if (position == 0){// 刷新
			return 0;
		} else if (position > 0 && position < this.getCount() - 1){// 返回微博的ID
			return allstatus.get(position - 1).getId();
		} else { // 选中了更多
			return -1;
		}
	}

	/**
	 * 动态给StatusAdapter添加数据
	 * @param adddata
	 */
	public void addMoreData(List<Status> adddata) {
//		this.allstatus.addAll(adddata);
		for (Iterator iterator = adddata.iterator(); iterator.hasNext();) {
			Status status = (Status) iterator.next();
			if(!this.allstatus.contains(status)){
				this.allstatus.add(status);
			}
		}
		this.notifyDataSetChanged();
	}

	
	private ViewHolder viewHolder = new ViewHolder();

	public View getView(int position, View convertView, ViewGroup parent) {
		if (position == 0)// 刷新
		{
			View weiboitem = LayoutInflater.from(context).inflate(
					R.layout.moreitemsview, null);
			TextView tv = (TextView) weiboitem.findViewById(R.id.tvItemContent);
			tv.setText("刷新");
			return weiboitem;
		} 
		
		else if (position == this.getCount() - 1)// 更多
		{
			View weiboitem = LayoutInflater.from(context).inflate(
					R.layout.moreitemsview, null);
			TextView tv = (TextView) weiboitem.findViewById(R.id.tvItemContent);
			tv.setText("更多");
			return weiboitem;
		}
		
		else 
		{
			View weiboitem = null;

			if (convertView != null && convertView.findViewById(R.id.tvItemName) != null) {
				Log.d("getview", "doGetView-------get TextView-----------" + position);
				weiboitem = convertView;
			} else {
				Log.d("getview", "doGetView-------new TextView-----------" + position);
				// 把xml布局文件变成View对象
				weiboitem = LayoutInflater.from(context).inflate(R.layout.itemview_recommend_status, null);
			}
			
			this.initWeiboHoler(weiboitem);
			Status status = allstatus.get(position-1);
			Status retweetedStatus = status.getRetweetedStatus();
		    User user = status.getUser();
		    int userId = user.getId();
		    String bmiddlePic = status.getBmiddle_pic();
		    
			viewHolder.tvTweet_form.setText(
					 new StringBuffer()
					 .append("来自:")
					 .append(WeiboUtil.getComeFromSource(status.getSource()).toString()));
			 
			
			viewHolder.tvItemName.setText(user.getName());
			viewHolder.tvItemContent.setText(status.getText());
			
			if(WeiboUtil.strIsNotEmpty(bmiddlePic)){
				viewHolder.contentPic.setVisibility(View.VISIBLE);
				if(MainService.getAllImage(status.getId()) != null){
					////System.out.println("++++++++++图片出来了嘛++++++++++++");
					viewHolder.contentPic.setImageBitmap(MainService.getAllImage(status.getId()));
				}else{
					//设置缺省的图片
					viewHolder.contentPic.setImageResource(R.drawable.loading);
				}
			}else{
				viewHolder.contentPic.setVisibility(View.GONE);
			}
			
			viewHolder.tvItemDate.setText(WeiboUtil.getCreateAt(status.getCreatedAt()));
//			// 是否实名认证
			//******************************************
			//这里有一个BUG待解决  此BUG于2010-5-16日修改 --->新浪SDK的问题
			//******************************************
			if (user.isVerified()) {
				Log.d("ok", "ok isVerified");
				viewHolder.ivItemV.setVisibility(View.VISIBLE);
			} else {
				viewHolder.ivItemV.setVisibility(View.GONE);
			}
//			// 判断时间图片
//			if (status.getThumbnail_pic() != null) {
//				viewHolder.ivItemPic.setVisibility(View.VISIBLE);
//			} else {
//				viewHolder.ivItemPic.setVisibility(View.GONE);
//			}
//			
			// 头像
			// 如果头像已经下载
			if (MainService.allIcon.get(userId) != null) {
				viewHolder.ivItemPortrait.setImageBitmap(MainService.allIcon.get(userId));
			} else {// 设定缺省的图片
				viewHolder.ivItemPortrait.setImageResource(R.drawable.portrait);
			}
			// 转发内容
			if (retweetedStatus != null) {
				viewHolder.subLayout.setVisibility(View.VISIBLE);
				String txt = retweetedStatus.getText();
				viewHolder.tvItemSubContent.setText(txt);
				String retweetedStatusBmiddlePic = retweetedStatus.getBmiddle_pic();
				
				User retweetdUser = retweetedStatus.getUser();
				int retweetdUserId = retweetdUser.getId();
				long retweetedStatusId = retweetedStatus.getId();
				if(WeiboUtil.strIsNotEmpty(retweetedStatusBmiddlePic)){
					viewHolder.subContentPic.setVisibility(View.VISIBLE);
					if(MainService.getAllImage(retweetedStatusId) != null){
						viewHolder.subContentPic.setImageBitmap(MainService.getAllImage(retweetedStatusId));
						////System.out.println("++++++++++图片出来了嘛++++++++++++");
//						viewHolder.subContentPic.setImageBitmap(MainService.getAllImage(retweetedStatusId).get());
					}else{
						//设置缺省的图片
					     viewHolder.subContentPic.setImageResource(R.drawable.loading);
					}
				}else{
					viewHolder.subContentPic.setVisibility(View.GONE);
				}
				
				
			} else {
				viewHolder.subLayout.setVisibility(View.GONE);
			}
			
			float weight = WeiboUtil.getUserRecommendWeight(user.getFollowersCount(), user.getFriendsCount(), user.getStatusesCount());
			
			//渲染控件
			WeiboUtil.setRecommendUserWeightToWiew(
					weight,
					viewHolder.btnUserRecommendWeight);
			////System.out.println("转发：" + status.getRetweetedNum());
			////System.out.println("评论：" + status.getCommNum());
			//微博信息的推荐权值
			weight = WeiboUtil.getStatusRecommendWeight(status.getRetweetedNum(), status.getCommNum());
			//渲染控件
			WeiboUtil.setRecommendStatusWeightToWiew(
					weight,
					viewHolder.btnStatusRecommendWeight);
			return weiboitem;
		}
	}

	private void initWeiboHoler(View weiboitem) {
		// 昵称
		viewHolder.ivItemPortrait = (ImageView) weiboitem
				.findViewById(R.id.ivItemPortrait);
		viewHolder.tvItemName = (TextView) weiboitem.findViewById(R.id.tvItemName);
		viewHolder.ivItemV = (ImageView) weiboitem.findViewById(R.id.ivItemV);
		viewHolder.tvItemDate = (TextView) weiboitem.findViewById(R.id.tvItemDate);
		viewHolder.ivItemPic = (ImageView) weiboitem.findViewById(R.id.ivItemPic);
		viewHolder.tvItemContent = (TextView) weiboitem.findViewById(R.id.tvItemContent);
		viewHolder.contentPic = (ImageView) weiboitem.findViewById(R.id.contentPic);
		viewHolder.subLayout = weiboitem.findViewById(R.id.subLayout);
		viewHolder.tvItemSubContent = (TextView) viewHolder.subLayout.findViewById(R.id.tvItemSubContent);
		viewHolder.subContentPic = (ImageView) viewHolder.subLayout.findViewById(R.id.subContentPic);
		viewHolder.tvTweet_form = (TextView) weiboitem.findViewById(R.id.tweet_form);
		
		viewHolder.btnUserRecommendWeight = (Button) weiboitem.findViewById(R.id.btnUserRecommendWeight);
		viewHolder.btnStatusRecommendWeight = (Button) weiboitem.findViewById(R.id.btnStatusRecommendWeight);
	
	}
	
	private class ViewHolder {
		
		ImageView ivItemPortrait;// 头像 有默认值
		
		TextView tvItemName;// 昵称
		
		ImageView ivItemV;// 新浪认证 默认gone
		
		TextView tvItemDate;// 时间
		
		ImageView ivItemPic;// 时间图片 不用修改
		
		TextView tvItemContent;// 内容
		
		ImageView contentPic;// 自己增加的内容图片显示的imgView
		
		View subLayout;// 回复默认gone
		
		TextView tvItemSubContent;// 回复内容 subLayout显示才可以显示
		
		ImageView subContentPic;// 自己增加的主要显示回复内容的图片。subLayout显示才可以显示
	    
		TextView tvTweet_form; //微博来自
		
		Button btnUserRecommendWeight;  //用户推荐权值
		 
		Button btnStatusRecommendWeight;  //微博信息推荐权值
	}
}